// pages/homestayDetails/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product_id: null,
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    detail: null,
    banner: [],
    facility: [],
    commentList: [],
    param: {},
    discounts: [],
    showDiscount: false,
  },
  goSubmitOrder() {
    wx.navigateTo({
      url: '/pages/submitOrder/index?product_id=' + this.data.param.product_id
    })
  },
  goPoster() {
    wx.navigateTo({
      url: '/pages/homestayDetails/poster/poster?product_id=' + this.data.param.product_id
    })
  },

  /**
   * 获取详情
   */
  getProductDetail() {
    app.loading()
    let _this = this;
    let param = _this.data.param;
    if (_this.data.product_id) {
      param.product_id = _this.data.product_id;
    }
    app.HttpsRequest('product/detail', 'GET', param).then(ret => {
      wx.hideLoading();
      if (ret) {
        _this.setData({
          detail: ret.data
        });
        this.getProductDiscount();
      }
    })
  },

  /**
   * 获取折扣
   */
  getProductDiscount() {
    app.loading()
    let _this = this;
    let param = _this.data.param;
    if (_this.data.product_id) {
      param.product_id = _this.data.product_id;
    }
    app.HttpsRequest('discount/index', 'GET', param).then(ret => {
      wx.hideLoading();
      if (ret) {
        _this.setData({
          discounts: ret.data
        })
      }
    })
  },

  /**
   * 展示隐藏
   */
  handleShowDiscount() {
    this.setData({
      showDiscount: !this.data.showDiscount
    })
  },

  /**
   * 添加收藏；
   */
  doCollect() {
    let _this = this;
    app.HttpsRequest('collect/create', 'GET', {
      product_id: _this.data.param.product_id,
    }).then(ret => {
      _this.getProductDetail()
    })
  },

  openMap() {
    let _this = this;
    wx.openLocation({
      latitude: parseFloat(_this.data.detail.latitude), // 纬度，范围为-90~90，负数表示南纬
      longitude: parseFloat(_this.data.detail.longitude), // 经度，范围为-180~180，负数表示西经
      scale: 28, // 缩放比例
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    options.product_id = options.product_id ? options.product_id : 8;
    _this.setData({
      param: options
    })
    if (options && options.scene) {
      const scene = decodeURIComponent(options.scene);
      _this.setData({
        product_id: options.product_id
      });
    }

  },

  /**
   * 打电话
   */
  callMobile() {
    if (this.data.detail.service_mobile) {
      wx.makePhoneCall({
        phoneNumber: this.data.detail.service_mobile
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    _this.getProductDetail()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})